package com.jackrain.nea.fc.ps.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.ps.api.ViewCproQueryCmd;
import com.jackrain.nea.fc.ps.mapper.ViewCproMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * FC-商品档案接口-服务开发
 *
 * @author wuwenxue
 * @date 2017/10/20
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ps")
public class ViewCproQueryCmdImpl extends CommandAdapter implements ViewCproQueryCmd {
    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ViewCproMapper viewCproMapper = ApplicationContextHandle.getBean(ViewCproMapper.class);
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("ViewCproQueryCmdImpl--param-->{}",param);

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        Object[] id = param.getJSONArray("IDS").toArray();
        JSONArray jsonArray = new JSONArray();
        List<HashMap> list = viewCproMapper.findAllPro(id);
        for (HashMap<String, Object> hashMap : list) {
            //外键关联：品牌
            Long brand = TypeUtils.castToLong(hashMap.get("PS_C_BRAND_ID"));
            if (null != brand) {
                String ecode = viewCproMapper.findByBrandID(brand);
                hashMap.put("PS_C_BRAND_ID", ecode);
            }

            String[] str = new String[]{"UNIT", "PURCHASEMODE", "PROYEAR", "PROSEA", "SEX", "LARGECLASS", "SUPBRAND",
                    "PROSOURCE", "PURNATURE", "BUYER", "PRONATURE", "PROMOTIONTYPE", "PROLINE", "POPULAR", "STYLE",
                    "DETAILS", "BUYPATTERNER", "SHELFCODE", "PRICEBAND", "PROMONTH", "PROBAND", "FABRIC", "SERIES",
                    "SAFETECHCLASS", "PROSTANDARD", "FOLLOWER", "NEWPROER", "MDLARGECLASS", "MDMIDDLECLASS",
                    "PROSTANDARD2", "COMPOSITION", "NUMDIM1", "DESIGNER", "THEMESTORY", "NUMDIM2", "NUMDIM3", "NUMDIM4",
                    "NUMDIM5", "NUMDIM6", "NUMDIM7", "NUMDIM8", "NUMDIM9", "NUMDIM10", "NUMDIM11", "NUMDIM12", "NUMDIM13",
                    "NUMDIM14", "NUMDIM15", "NUMDIM16", "NUMDIM17", "NUMDIM18", "NUMDIM19", "NUMDIM20", "NUMDIM21",
                    "NUMDIM22", "NUMDIM23", "NUMDIM24", "NUMDIM25", "NUMDIM26", "NUMDIM27"};
            //外键关联：商品属性值
            for (int i = 0; i < str.length; i++) {
                Long temp = TypeUtils.castToLong(hashMap.get(str[i]));
                if (null != temp) {
                    String ecode = viewCproMapper.findByID(temp);
                    hashMap.put(str[i], ecode);
                }
            }

            //外键关联配销中心表CP_C_STOREORG
            Long storeorg = TypeUtils.castToLong(hashMap.get("CP_C_DISTRIB_ID"));
            if (null != storeorg) {
                String ecdoe = viewCproMapper.findByDistrib(storeorg);
                hashMap.put("CP_C_DISTRIB_ID", ecdoe);
            }

            //外键关联供应商表CP_C_SUPPLIER
            Long supplier = TypeUtils.castToLong(hashMap.get("CP_C_SUPPLIER_ID"));
            if (null != supplier) {
                String ecode = viewCproMapper.findBySupplier(supplier);
                hashMap.put("CP_C_SUPPLIER_ID", ecode);
            }

            //外键关联用户表USERS OWNERID
            Long ownerId = TypeUtils.castToLong(hashMap.get("OWNERID"));
            if (null != ownerId) {
                String ename = viewCproMapper.findByUserId(ownerId);
                hashMap.put("OWNERID", ename);
            }

            //外键关联用户表USERS MODIFIERID
            Long modifierId = TypeUtils.castToLong(hashMap.get("MODIFIERID"));
            if (null != modifierId) {
                String ename = viewCproMapper.findByUserId(modifierId);
                hashMap.put("MODIFIERID", ename);
            }

            //默认存储仓库外键关联仓库档案CP_C_STORE
            Long store = TypeUtils.castToLong(hashMap.get("CP_C_WAREHOUSE_ID"));
            if (null != store) {
                String ecode = viewCproMapper.findByStore(store);
                hashMap.put("CP_C_WAREHOUSE_ID", ecode);
            }

            //配送中心外键关联仓库档案CP_C_STORE
            Long discenter = TypeUtils.castToLong(hashMap.get("DISCENTER"));
            if (null != discenter) {
                String ecode = viewCproMapper.findByStore(discenter);
                hashMap.put("DISCENTER", ecode);
            }

            String yes = "Y";
            String no = "N";
            //是否可用
            String isactive = TypeUtils.castToString(hashMap.get("ISACTIVE"));
            if (yes.equals(isactive)) {
                hashMap.put("ISACTIVE", 0);
            } else if (no.equals(isactive)) {
                hashMap.put("ISACTIVE", 1);
            }

            //最小批量 (取整传输)
            BigDecimal minlotsize = TypeUtils.castToBigDecimal(hashMap.get("MINLOTSIZE"));
            if (null != minlotsize) {
                hashMap.put("MINLOTSIZE", minlotsize.setScale(0, BigDecimal.ROUND_HALF_UP));
            }

            //供应商
            Long proID = TypeUtils.castToLong(hashMap.get("ID"));
            List<String> supplierList = viewCproMapper.selBySupplier(proID);
            if (null != supplierList && supplierList.size() > 0) {
//                log.info("supplierList-->" + supplierList.toString());
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < supplierList.size(); i++) {
                    sb.append(supplierList.get(i));
                    sb.append(",");
                }
                sb.deleteCharAt(sb.length() - 1);
//                log.info("sb-->" + sb.toString());
                hashMap.put("Extra_Provider", sb.toString());
            }
            hashMap.remove("ID");

            JSONObject js = new JSONObject(hashMap);
            JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param1);

        }
//        log.debug("ps_c_pro query results-->" + jsonArray.toString());
        vh.put("data", jsonArray);
        return vh;
    }
}
